package com.iraytek.rtspdemo.util;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.fragment.app.Fragment;

import java.util.ArrayList;
import java.util.List;

public class PermissionHelper {
    public static boolean hasPermissions(Context context, String... permissions) {
        int status = 0;//PackageManager.PERMISSION_GRANTED
        for (String permission : permissions) {
            status |= ActivityCompat.checkSelfPermission(context, permission);
        }
        return status == 0;
    }

    public static void checkPermissions(AppCompatActivity activity, String[] permissions, PermissionGrantListener listener) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) return;
        List<String> list = new ArrayList<>(4);
        for (String permission : permissions) {
            if (activity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                list.add(permission);
            }
        }
        if (list.size() > 0) {
            PermissionFragment fragment = new PermissionFragment();
            fragment.permissions = list.toArray(new String[0]);
            fragment.setPermissionGrantListener(listener);
            activity.getSupportFragmentManager().beginTransaction().add(fragment, fragment.getTag()).commit();
        } else {
            listener.onAllPermissionGranted();
        }
    }

    public static class PermissionFragment extends Fragment {
        private static final int CODE = 100;
        private PermissionGrantListener permissionGrantListener;
        private int retryCount;
        String[] permissions;

        public void setPermissionGrantListener(PermissionGrantListener permissionGrantListener) {
            this.permissionGrantListener = permissionGrantListener;
        }

        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestPermissions(permissions, CODE);
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            if (requestCode == CODE) {
                List<String> list = new ArrayList<>(4);
                for (int i = 0; i < permissions.length; i++) {
                    if (grantResults[i] != PackageManager.PERMISSION_GRANTED) {
                        list.add(permissions[i]);
                    }
                }
                if (list.size() > 0) {
                    if (retryCount < 2) {
                        requestPermissions(list.toArray(new String[0]), CODE);
                        retryCount++;
                    }
                } else {
                    if (permissionGrantListener != null) {
                        permissionGrantListener.onAllPermissionGranted();
                    }
                }
            }
        }
    }

    public interface PermissionGrantListener {
        void onAllPermissionGranted();
    }
}
